import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../utils/color_utils.dart';
import '../../utils/log_utils.dart';

class CartNumberView extends StatefulWidget {

  final ValueChanged<int> onValueAction;
  int beginCount;
  final double? width;
  final double? height;

  CartNumberView({Key? key, required this.onValueAction, required this.beginCount, this.width = 180, this.height = 44}) : super(key: key);

  @override
  State<CartNumberView> createState() => _CartNumberViewState();
}

class _CartNumberViewState extends State<CartNumberView> {

  TextEditingController numberTextEditingController = TextEditingController();
  FocusNode numberFocusNode = FocusNode();
  int num = 1;

  @override
  void initState() {
    num = widget.beginCount;
    numberTextEditingController.text = widget.beginCount.toString();

    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: widget.height,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: ThemeColors.clffffffff,
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: ThemeColors.clff9f9f9f, width: 1),
      ),
      child: TextField(
        keyboardType: TextInputType.number,
        textAlign: TextAlign.center,
        textAlignVertical: TextAlignVertical.center,
        style: const TextStyle(
          fontSize: 20,
          color: ThemeColors.clff202735,
          fontWeight: FontWeight.bold
        ),
        // textAlignVertical: TextAlignVertical.center,
        controller: numberTextEditingController,
        focusNode: numberFocusNode,
        textInputAction: TextInputAction.done,
        obscureText: false,
        //是否以密码框的方式显示文本
        inputFormatters: [
          // LengthLimitingTextInputFormatter(11), //限制长度
          FilteringTextInputFormatter.allow(
              RegExp("[0-9]")), //只能输入大小写字母
        ],
        onChanged: (value) {
           widget.beginCount = int.parse(value);
           widget.onValueAction(widget.beginCount);
        },
        // onEditingComplete: () {   //添加这个之后点击键盘上的完成按钮键盘不会自动收起
        //   LogUtil.println("onEditingComplete");
        // },
        onSubmitted: (value) {
          LogUtilTool.println("onSubmitted $value");
        },
        onTap: () {
          LogUtilTool.println("onTap");
        },
        decoration: InputDecoration(
          contentPadding: const EdgeInsets.only(left: 0),
          isDense: true,
          filled: true,
          fillColor: Colors.transparent,
          // hintText: '请输入密码',
          // hintStyle: const TextStyle(
          //     fontSize: 16,
          //     color: ThemeColors.clff9f9f9f
          // ),
          border: InputBorder.none,
          prefixIcon: numIconView(true, () {
            if (num == 1) {
              return ;
            }
            num--;
            numberTextEditingController.text = num.toString();
            widget.onValueAction(num);
          }),
          prefixIconConstraints: const BoxConstraints(
            minWidth: 40,
            maxWidth: 40,
          ),
          suffixIcon: numIconView(false, () {
            num++;
            numberTextEditingController.text = num.toString();
            widget.onValueAction(num);
          }),
          suffixIconConstraints: const BoxConstraints(
            minWidth: 40,
            maxWidth: 40,),
        ),
      ),
    );
  }

  Widget numIconView(bool isLeft, clickAction) {
     return GestureDetector(
       onTap: () {
         clickAction();
       },
       child: Container(
         height: 40,
         color: Colors.transparent,
         alignment: Alignment.center,
         child: Image.asset(!isLeft ? 'assets/images/my/cart_number_add.png' : 'assets/images/my/cart_number_reduce.png', width: 16, height: 16,),
       ),
     );
  }

}
